package com.cjh.mybatis11.bulider.xml;

import com.cjh.mybatis11.bulider.BaseBuilder;
import com.cjh.mybatis11.bulider.MapperBuilderAssistant;
import com.cjh.mybatis11.mapping.SqlCommandType;
import com.cjh.mybatis11.mapping.SqlSource;
import com.cjh.mybatis11.scriping.LanguageDriver;
import com.cjh.mybatis11.session.Configuration;
import org.dom4j.Element;

import java.util.Locale;

/**
 * @Descripton xml 语句构建器
 * @Author cjh
 * @Date 2023/6/12 14:41
 */
public class XMLStatementBuilder extends BaseBuilder {

    private MapperBuilderAssistant builderAssistant;
    private Element element;

    public XMLStatementBuilder(Configuration configuration, MapperBuilderAssistant builderAssistant, Element element) {
        super(configuration);
        this.builderAssistant = builderAssistant;
        this.element = element;


    }

    /**
     * //解析语句(select|insert|update|delete)
     * //<select
     * //  id="selectPerson"
     * //  parameterType="int"
     * //  parameterMap="deprecated"
     * //  resultType="hashmap"
     * //  resultMap="personResultMap"
     * //  flushCache="false"
     * //  useCache="true"
     * //  timeout="10000"
     * //  fetchSize="256"
     * //  statementType="PREPARED"
     * //  resultSetType="FORWARD_ONLY">
     * //  SELECT * FROM PERSON WHERE ID = #{id}
     * //</select>
     */
    public void parseStatementNode() {
        String id = element.attributeValue("id");
        //    参数类型
        String parameterType = element.attributeValue("parameterType");
        Class<?> parameterTypeClass = resolveAlias(parameterType);

        //外部应用resultMap
        String resultMap = element.attributeValue("resultMap");

        //    结果类型
        String resultType = element.attributeValue("resultType");
        Class<?> resultTypeClass = resolveAlias(resultType);
        // 获取命令类型(select|insert|update|delete)
        String nodeName = element.getName();
        SqlCommandType sqlCommandType = SqlCommandType.valueOf(nodeName.toUpperCase(Locale.ENGLISH));
        // 获取默认语言驱动器
        Class<?> langClass = configuration.getLanguageRegistry().getDefaultDriverClass();
        LanguageDriver langDriver = configuration.getLanguageRegistry().getDriver(langClass);

        //解析成sqlsource DynamicSqlSource /RawSqlSource
        SqlSource sqlSource = langDriver.createSqlSource(configuration, element, parameterTypeClass);

        //调用助手类【本节新添加 便于统一处理参数的包装】
        builderAssistant.addMappedStatement(id, sqlSource, sqlCommandType, parameterTypeClass, resultMap, resultTypeClass, langDriver);

    }
}
